package com.ruoyi.uni.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.uni.domain.DcGoods;
import com.ruoyi.uni.domain.DcOrder;
import com.ruoyi.uni.mapper.DcGoodsMapper;
import com.ruoyi.uni.mapper.DcOrderMapper;
import com.ruoyi.uni.service.IDcOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单管理Service业务层处理
 * 
 * @author hx
 * @date 2024-04-18
 */
@Service
public class DcOrderServiceImpl implements IDcOrderService
{
    @Autowired
    private DcOrderMapper dcOrderMapper;

    @Autowired
    private DcGoodsMapper dcGoodsMapper;
    /**
     * 查询订单管理
     * 
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public DcOrder selectDcOrderById(String id)
    {
        return dcOrderMapper.selectDcOrderById(id);
    }

    /**
     * 查询订单管理列表
     * 
     * @param dcOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<DcOrder> selectDcOrderList(DcOrder dcOrder)
    {
        List<DcOrder> dcOrders = dcOrderMapper.selectDcOrderList(dcOrder);

        for (DcOrder order:dcOrders){
            List<DcGoods> goodIds= JSONArray.parseArray(order.getGoodsId(),DcGoods.class);
            List<DcGoods> newGood=new ArrayList<>();

            for (DcGoods good : goodIds){
                DcGoods dcGoods = dcGoodsMapper.selectDcGoodsById(good.getId());
                dcGoods.setNum(good.getNum());
                newGood.add(dcGoods);
            }
            order.setDcGoodsList(newGood);
            order.setGoodsId(null);
        }

        return dcOrders;
    }

    /**
     * 新增订单管理
     * 
     * @param dcOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertDcOrder(DcOrder dcOrder)
    {
        dcOrder.setCreateTime(DateUtils.getNowDate());
        return dcOrderMapper.insertDcOrder(dcOrder);
    }

    /**
     * 修改订单管理
     * 
     * @param dcOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateDcOrder(DcOrder dcOrder)
    {
        return dcOrderMapper.updateDcOrder(dcOrder);
    }


    @Override
    public int updateStatusById(int status,String id){
        return dcOrderMapper.updateStatus(status, id);
    }
    /**
     * 批量删除订单管理
     * 
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteDcOrderByIds(String[] ids)
    {
        return dcOrderMapper.deleteDcOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     * 
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteDcOrderById(String id)
    {
        return dcOrderMapper.deleteDcOrderById(id);
    }
}
